﻿--[[
   @brief    descprtion: badge_control
   @author   hanmanhuang
   @version  1.0
   @date     2016-12-27
--]]

badge_control = 
{

    init = function()
        base_utility.log("badge_control init");
        local tpf_ui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        if nil == tpf_ui then
            return;
        end;
        
        tpf_ui:AddLuaListener(ierd_tgp.game_library.JSON_CFG_UPDATED, "badge_control.on_get_json_cfg");

        ierd_tgp.tpf_ui.Require("badge\\model.lua");
        ierd_tgp.tpf_ui.Require("badge\\view.lua");

        ierd_tgp.tpf_ui.Require("badge\\badge_detail\\control.lua");
        ierd_tgp.tpf_ui.Require("badge\\badge_points\\control.lua");
        ierd_tgp.tpf_ui.Require("badge\\badge_levelup\\control.lua");
    end;

    inner_log = function (msg, core_msg)
        base_utility.log(string.format("[badge_control] %s", msg), core_msg);
    end;

    on_get_json_cfg = function(event_id, json_str)
        local config_table = base_utility.json_to_table(json_str);
        if config_table == nil then
            return;
        end;
        if config_table["json"] ~= nil and type(config_table["json"]) == 'table'
            and config_table["json"]["data"] ~= nil then
            if config_table["name"] == "connoisseur_badge_cfg_info" then
                badge_control.inner_log("on_get_json_cfg connoisseur_badge_cfg_info");
                badge_model.con_badge_oss_cfg = config_table["json"]["data"];
            end
        end
    end;
    
    -- 打开徽章相关窗口
    open_badge_win = function(event_id, json_param)
        if nil == json_param then
            return;
        end;
        badge_control.inner_log("open_badge_win"..json_param);
        local param = base_utility.json_to_table(json_param);
        if param.type ~= badge_model.badge_jump_main_type then
            return;
        end
        if param.sub_page == badge_model.badge_jump_detail_type then
            local web_param = {
                url = badge_control.get_badge_detail_page_url(param);
                width = 620;
                height = 400;
                title_in_web = true;
                use_ptlogin = true;
                show_loading_page = true;
                reload = true;
                caption = LoadStr("BADGE_DETAIL");
            };
            comm_center.call_service("i_service_show_simple_web_window", web_param);
        elseif param.sub_page == badge_model.badge_jump_points_type then
            local web_param = {
                url = badge_control.get_badge_points_page_url(param);
                width = 719;
                height = 511;
                title_in_web = true;
                use_ptlogin = true;
                show_loading_page = true;
                reload = true;
                caption = LoadStr("BADGE_GAIN_CREDITS");
            };
            comm_center.call_service("i_service_show_simple_web_window", web_param);
        elseif param.sub_page == badge_model.badge_jump_levelup_type then
            -- 屏蔽可能的入口
            -- badge_control.open_badge_levelup_win(event_id, json_param);
        end;
    end;

    -- 获得徽章详情页地址
    get_badge_detail_page_url = function(page_info)
        local url = badge_model.get_badge_detail_page_url(page_info);
        local url_login = web_utility.make_login_status_url(url, login_model.main_uin);
        return url_login;
    end;

    -- 获得徽章积分页地址
    get_badge_points_page_url = function(page_info)
        local url = badge_model.get_badge_points_page_url(page_info);
        local url_login = web_utility.make_login_status_url(url, login_model.main_uin);
        return url_login;
    end;

    -- 获得升级页地址
    get_badge_levelup_page_url = function(page_info)
        local url = badge_model.get_badge_levelup_page_url(page_info);
        local url_login = web_utility.make_login_status_url(url, login_model.main_uin);
        return url_login;
    end;

    open_badge_levelup_win = function(event_id, json_param)
        if nil == json_param then
            return;
        end;
        base_utility.log("jadonsuntest = ".. json_param);
        badge_control.inner_log("open_badge_levelup_win"..json_param);
        local param = base_utility.json_to_table(json_param);
        if param.type ~= badge_model.badge_jump_main_type or 
           param.sub_page ~= badge_model.badge_jump_levelup_type then
            return;
        end;
        badge_levelup_control.open_win(param);
    end;

    -- 打开鉴赏家升级弹窗
    handle_judge_tips = function(judge_level, judge_level_next_need)
        local param = {};
        param.type = "badge";
        param.sub_page = "BADGE_LEVELUP";
        param.badge_level = judge_level;
        param.badge_level_next_need = judge_level_next_need;
        local cmd = base_utility.table_to_json(param);
        event_center.send_event(badge_event.OPEN_BADGE_WIN, cmd);
    end;
};

badge_control.init();